package com.bitcat.firstapp.demo.newslistdemo

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bitcat.firstapp.databinding.ItemRefreshListBinding

class RefreshAdapter(private val list:MutableList<RefreshDemoItem>):RecyclerView.Adapter<RefreshAdapter.VH>() {



    class VH(val binding:ItemRefreshListBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
       val binding = ItemRefreshListBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return VH(binding)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = list[position]
        holder.binding.apply {
            titleText.text = item.title
            contentText.text = item.content
        }
    }

    //下拉刷新
    fun refreshData(list:List<RefreshDemoItem>){
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }


    //加载更多
    fun loadMore(newItems:List<RefreshDemoItem>){
        val oldSize = list.size
        list.addAll(newItems)
        notifyItemRangeInserted(oldSize,newItems.size)
    }
}

data class RefreshDemoItem(
    val title: String,
    val content: String
)